package com.shire.shire_interface.service.account.impl;

import com.shire.shire_interface.common.utils.StrUtils;
import com.shire.shire_interface.constants.Constants;
import com.shire.shire_interface.dao.accounts.DeviceInfoDao;
import com.shire.shire_interface.model.account.DeviceInfo;
import com.shire.shire_interface.model.account.DeviceInfoExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@CacheConfig(cacheNames = {"device_info"})
@Service
public class DeviceInfoService {
    @Autowired
    private DeviceInfoDao deviceInfoDao;

    public int insertDeviceInfo(DeviceInfo deviceInfo) {
        return deviceInfoDao.insert(deviceInfo);
    }

    public int updateDeviceInfo(DeviceInfo deviceInfo) {
        return deviceInfoDao.updateByPrimaryKey(deviceInfo);
    }

    @Cacheable(key = "#p0")
    public DeviceInfo getDeviceInfoByUuid(String realUuid){
        DeviceInfoExample deviceInfoExample = new DeviceInfoExample();
        deviceInfoExample.createCriteria().andRealudidEqualTo(realUuid);
        List<DeviceInfo> deviceInfos = deviceInfoDao.selectByExample(deviceInfoExample);
        if (deviceInfos.size() > 0 ){
            return deviceInfos.get(0);
        }

        return null;
    }

    public DeviceInfo getAndSaveDeviceInfo(String realUdid, String deviceType, String ver, String sysVer, String ip){
        if(StrUtils.isBlank(realUdid)) return null;
        synchronized (realUdid.intern()) {
            String workEnv ="innet";// systemVariablesService.getVariablesValue("work.env", "innet");
//			System.out.println("workEnv = " + workEnv+", ip = " + ip + ", realUdid = " + realUdid);
//			if(workEnv.equals("innet") && !ip.startsWith("172.16")){
//				return null;
//			}
            DeviceInfo deviceInfo = getDeviceInfoByUuid(realUdid);
            if(deviceInfo==null){
                // 临时添加设备
                deviceInfo = new DeviceInfo();
                deviceInfo.setRealudid(realUdid);
                deviceInfo.setDevicemodel(deviceType);
                deviceInfo.setSystemversion(sysVer);
                deviceInfo.setLocalver(ver);
                deviceInfo.setStatus(Constants.DeviceStatus.ENABLE.ordinal());
                deviceInfo.setOnlinetime(new Date());
                deviceInfo.setCreatetime(new Date());
                deviceInfo.setIp(ip);
//                cacheService.putLocalLong(realUdid, deviceInfo);
                deviceInfoDao.insert(deviceInfo);
            }
            else{
                if(StrUtils.isNotBlank(deviceType)){
                    deviceInfo.setDevicemodel(deviceType);
                }
                if(StrUtils.isNotBlank(sysVer)){
                    deviceInfo.setSystemversion(sysVer);
                }
                if(StrUtils.isNotBlank(ver)){
                    deviceInfo.setLocalver(ver);
                }
                deviceInfo.setIp(ip);
                deviceInfo.setOnlinetime(new Date());
                deviceInfoDao.updateByPrimaryKey(deviceInfo);
            }
            return deviceInfo;
        }
    }

}
